package view;

import Broker.yahooJapanAuction.UrlExtractorFromList;
import Broker.yahooJapanAuction.UrlExtractorListener;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import model.db.table.BidHistory;

/**
 * Author: Raoux Etienne
 * Student login: jc218934
 * Student number: 12448190
 * email: etienne.raoux@my.jcu.edu.au
 * alt email: jc218934@gmail.com
 */
public class ItemView extends JFrame implements UrlExtractorListener
{
    private ArrayList<BidHistory> bidHistoryList;
    private final ItemView itemView;

    public ItemView()
    {
        itemView = this;
        initComponents();
        setSize(Toolkit.getDefaultToolkit().getScreenSize());
        validate();
    }

    public void start()
    {
        setVisible(true);
    }

    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        urlLabel = new javax.swing.JLabel();
        urlTextField = new javax.swing.JTextField();
        startButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        itemDisplayPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout());

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout());

        urlLabel.setText("URL");
        jPanel2.add(urlLabel, java.awt.BorderLayout.WEST);

        urlTextField.setText("http://auctions.yahoo.co.jp/start");
        jPanel2.add(urlTextField, java.awt.BorderLayout.CENTER);

        startButton.setText("Start");
        startButton.addActionListener(new StartListener());
        jPanel2.add(startButton, BorderLayout.EAST);

        jPanel1.add(jPanel2, BorderLayout.NORTH);

        jPanel3.setLayout(new BorderLayout());

        itemDisplayPanel.setLayout(new BoxLayout(itemDisplayPanel, BoxLayout.Y_AXIS));

        jScrollPane1.setViewportView(itemDisplayPanel);

        jPanel3.add(jScrollPane1);

        jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1);
        bidHistoryList = new ArrayList<BidHistory>();
    }

    private void setItemsList(ArrayList<BidHistory> bidHistoryList)
    {
        this.bidHistoryList = bidHistoryList;
        setDisplayedItems();
    }

    public void setDisplayedItems()
    {
        for (BidHistory bidHistory:bidHistoryList)
        {
            itemDisplayPanel.add(new ItemDisplayPanel(bidHistory));
        }
        itemDisplayPanel.validate();
    }

    public void addItem(BidHistory bidHistory)
    {
        bidHistoryList.add(bidHistory);
        itemDisplayPanel.add(new ItemDisplayPanel(bidHistory));
        validate();
    }

    public void changeViewToItemBidHistoryView(BidHistory bidHistory)
    {
        //this.setVisible(false);
        new ItemBidHistoryView(bidHistory);
    }

    private class StartListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            Thread thread = new Thread(new UrlExtractorFromList(itemView, "http://auctions.yahoo.co.jp/start"));
            thread.start();
        }
    }

    
    // Variables declaration - do not modify
    private javax.swing.JButton startButton;
    private javax.swing.JLabel urlLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel itemDisplayPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField urlTextField;
    // End of variables declaration
}
